package com.kfm.student.controller;

import com.alibaba.druid.support.logging.Log;
import com.kfm.student.domain.User;
import com.kfm.student.service.UserService;
import com.kfm.student.util.Contains;
import com.wf.captcha.utils.CaptchaUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
@Slf4j
@RestController
public class UserController {

    @Autowired
    private UserService service;



    @GetMapping({"/","login"})
    public ModelAndView login(){
        log.info("访问登录页面");
        return new ModelAndView("login");
    }


    @PostMapping("/post")
    public ModelAndView doLogin(User user, HttpSession session){
        ModelAndView mv = new ModelAndView();

        if (CaptchaUtil.ver()) {

        }
        User login = service.login(user);
        if (login != null){
            //成功
            session.setAttribute(Contains.LOGIN_FLAG,login);
            mv.setViewName("redirect:index");
        }else {
            //失败 重新登录
            mv.addObject("msg","用户名或密码错误");
        }
        return mv;
    }
}
